﻿/****************************** Module Header ******************************\
 * Module Name:  ClientSoapHelper.cs
 * Module Version:  1.0.0 (2012-9-29)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to process the wcf.
 * 
 * # 2012-10-18 解决网络突然中断无法提示的问题.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.ServiceModel;

namespace UCF.Core
{
    public class ClientSoapHelper<T> where T : ICommunicationObject, new()
    {
        /// <summary>
        /// 网络异常处理方法
        /// </summary>
        /// <example>
        /// private void ExceptionHandler()
        /// {
        ///     if (MessageBox.Show("无法连接服务器，请检查您的网络。用户分部请检查VPN。", "警告", MessageBoxButtons.RetryCancel) != DialogResult.Retry) Environment.Exit(0) ;
        ///     System.Threading.Thread.Sleep(5000);
        ///     SRC.Current.Test();
        ///     SRC.Close();
        /// }
        /// </example>
        public static Action ExceptionHandler { get; set; }
        public static string IPAddress { get; set; }
        private static T client;
        public static bool Excepting;

        /// <summary>
        /// 获取当前通信服务隧道实例
        /// </summary>
        public static T Current
        {
            get
            {
                if (client == null || client.State != CommunicationState.Opened)
                {
                    try
                    {
                        client = new T();
                        System.Reflection.MethodInfo mi = typeof(T).GetMethod("Test");
                        if (mi != null)
                        {
                            // public bool Test() { try{ doConnectionTest();return true; }catch{return false;} }
                            if (!(bool)mi.Invoke(client, null))
                            {
                                if (!Excepting) ExceptionHandler();
                                Excepting = true;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ExceptionHandler == null)
                        {
                            Excepting = true;
                            throw new Exception("网络连接失败,请检查您的网络状态!" + ex.Message);
                        }
                        else
                        {
                            if (!Excepting) ExceptionHandler();
                            Excepting = true;
                        }
                    }
                }
                else
                {
                    try
                    {
                        System.Reflection.MethodInfo mi = typeof(T).GetMethod("Test");
                        if (mi != null)
                        {
                            if (!(bool)mi.Invoke(client, null))
                            {
                                if (!Excepting) ExceptionHandler();
                                Excepting = true;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ExceptionHandler == null)
                        {
                            throw new Exception("网络连接失败,请检查您的网络状态!" + ex.Message);
                            Excepting = true;
                        }
                        else
                        {
                            ExceptionHandler();
                        }
                    }
                }
                return client;
            }
        }

        /// <summary>
        /// 关闭隧道,释放服务器资源
        /// </summary>
        public static void Close()
        {
            try
            {
                if (client.State != CommunicationState.Faulted) client.Close();
            }
            catch
            {
                client.Abort();
            }
        }
    }
}
